/**
 * 
 */
package com.hfjyz.registration.servlet;

import java.util.List;

import javax.servlet.ServletContextEvent;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;

import com.hfjyz.registration.dao.OrderInfoDao;
import com.hfjyz.registration.model.OrderInfo;
import com.hfjyz.registration.service.MasterService;
import com.hfjyz.registration.service.OrderInfoService;
import com.hfjyz.registration.task.OrderPaySearchingTask;
import com.hfjyz.registration.task.TaskManager;
import com.hfjyz.registration.util.ExamProperties;
import com.hfjyz.registration.util.MasterUtil;

/**
 *
 */
public class ExamContextLoaderListener extends ContextLoaderListener {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.context.ContextLoaderListener#contextInitialized
	 * (javax.servlet.ServletContextEvent)
	 */
	@Override
	public void contextInitialized(ServletContextEvent event) {
		super.contextInitialized(event);
		WebApplicationContext currentWebApplicationContext = ContextLoader
				.getCurrentWebApplicationContext();
		MasterService masterService = (MasterService) currentWebApplicationContext.getBean("masterService");
		try {
			MasterUtil.getInstance().init(masterService.listAll());
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			OrderInfoService orderInfoService = (OrderInfoService) currentWebApplicationContext.getBean("orderInfoService");
			OrderInfoDao orderInfoDao = (OrderInfoDao) currentWebApplicationContext.getBean("orderInfoDao");
			ExamProperties examProperties = (ExamProperties) currentWebApplicationContext.getBean("examProperties");
			List<OrderInfo> orders = orderInfoDao.findByState(3);
			for (OrderInfo order : orders) {
				OrderPaySearchingTask task = new OrderPaySearchingTask(
						order.getId(), orderInfoService, examProperties);
				new TaskManager(task).start();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.context.ContextLoaderListener#contextDestroyed
	 * (javax.servlet.ServletContextEvent)
	 */
	@Override
	public void contextDestroyed(ServletContextEvent event) {
		super.contextDestroyed(event);
	}

}
